﻿#include "MyToolButton.h"
#include <QMouseEvent>
#include <QMenu>
#include "Global.h"
#include "UNodeManager.h"
#include "UPropertyNode.h"
MyToolButton::MyToolButton(CBaseNode *pNode, QButtonGroup *pGroup, QWidget *parent)
	: QToolButton(parent)
{
	m_pNode = pNode;
	setContextMenuPolicy(Qt::CustomContextMenu);
	connect(this, &QWidget::customContextMenuRequested, this, &MyToolButton::showContextMenu);
	initUi();
}

MyToolButton::~MyToolButton()
{
	m_pNode = NULL;
}

void MyToolButton::mouseDoubleClickEvent(QMouseEvent * event)
{
	if (m_pNode->getType() == CBaseNode::enNode)
	{
		emit sign_doubleClicked(m_pNode);
	}
	else if (m_pNode->getType() == CBaseNode::enDevice)
	{
		QWidget *pWidget = ((CBaseDevice *)m_pNode)->getDevice()->getDeviceRunUi();
		if (pWidget == nullptr)
		{
			return;
		}
		pWidget->setParent(NULL);
		QPoint globalPos(100, 100);
		QPoint localPos = pWidget->parentWidget()->mapFromGlobal(globalPos);
		pWidget->move(localPos);
		pWidget->show();
	}
}

void MyToolButton::mousePressEvent(QMouseEvent * event)
{
	if (event->button() == Qt::LeftButton)
	{
		emit sign_clicked(m_pNode);
	}
	QToolButton::mousePressEvent(event); // 调用基类 QToolButton 事件处理
}

void MyToolButton::initUi()
{
	setStyleSheet(R"(
        QToolButton {
        border-radius: 10px;     
		font: 8pt "阿里巴巴普惠体 L";
		border: none;   /* 去掉边框 */
		background-color: transparent;      /* 背景透明 */
		border: none;                       /* 无边框 */
		}
		QToolButton:hover {
		 background-color: rgb(87, 163, 176);
		}

		QToolButton:checked {
				 background-color: rgb(87, 163, 176);
			color: white;
		}
       )");
	if (m_pNode->getType() == CBaseNode::enNode)
	{
		setIcon(QIcon(":/SmartControl/Resource/files.png"));
		setIconSize(QSize(90, 90));
	}
	else if (m_pNode->getType() == CBaseNode::enDevice)
	{
		CBaseDevice* pDevice = (CBaseDevice*)m_pNode;
		QString sDllName = pDevice->getDllName();
		QString sIconPath = ":/SmartControl/Resource/robot.png";
		if (sDllName == "Robot")
		{
			sIconPath= ":/SmartControl/Resource/robot.png";
		}
		else if (sDllName == "Belt")
		{
			sIconPath = ":/SmartControl/Resource/belt.png";
		}
		else if (sDllName == "Roller")
		{
			sIconPath = ":/SmartControl/Resource/roller.png";
		}
		else if (sDllName == "SensorSeriesDevice")
		{
			sIconPath = ":/SmartControl/Resource/szw.png";
		}
		else if (sDllName == "CorrectDevice")
		{
			sIconPath = ":/SmartControl/Resource/beltReality.png";
		}
		else if (sDllName == "HKAIDevice")
		{
			sIconPath = ":/SmartControl/Resource/AI.png";
		}
		setIcon(QIcon(sIconPath));
		setIconSize(QSize(80, 80));
	}
	setText(m_pNode->getName());
	setCheckable(true);  
	setToolButtonStyle(Qt::ToolButtonTextUnderIcon); 
}

void MyToolButton::showContextMenu(const QPoint & pos)
{
	if (m_pNode == NULL)
	{
		return;
	}
	if (m_pNode->getType() == CBaseNode::enNode)
	{
		QMenu* menu = new QMenu(this);
		menu->addAction(QIcon(":/SmartControl/Resource/toolbar/open.png"),A2T("打开(O)..."), this, [=]()
		{ 
			QMetaObject::invokeMethod(this, [=]() 
			{
				emit sign_doubleClicked(m_pNode);
			}, Qt::QueuedConnection);  // 关键：排队触发
		});
		menu->addSeparator();
		//menu->addAction(A2T("属性"), this, [=]() {});
		//menu->addAction(QIcon(":/SmartControl/Resource/toolbar/set.png"), A2T("参数配置(S)..."), this, [=]() {});
		menu->addSeparator();
		menu->addAction(QIcon(":/SmartControl/Resource/toolbar/delete.png"),A2T("删除(D)"), this, [=]() 
			{
				QMetaObject::invokeMethod(this, [=]()
					{
						emit sign_Delete(m_pNode);
					}, Qt::QueuedConnection);  // 关键：排队触发
			});
		menu->addSeparator();
		menu->addAction(A2T("属性"), this, [=]() 
			{
				UPropertyNode dlg(m_pNode,this);  // this 是父窗口
				dlg.exec();
			});
		menu->exec(mapToGlobal(pos));
		
	}
	else if (m_pNode->getType() == CBaseNode::enDevice)
	{
		QMenu* menu = new QMenu(this);
		menu->addAction(QIcon(":/SmartControl/Resource/toolbar/open.png"), A2T("打开(O)..."), this, [=]() 
			{ 
				QWidget* pWidget = ((CBaseDevice*)m_pNode)->getDevice()->getDeviceRunUi();
				if (pWidget == nullptr)
				{
					return;
				}
				pWidget->show();
			});
		menu->addSeparator();
		//menu->addAction(A2T("属性"), this, [=]() {});
		menu->addAction(QIcon(":/SmartControl/Resource/toolbar/set.png"), A2T("参数配置(S)..."), this, [=]() { ((CBaseDevice *)m_pNode)->getDevice()->getDeviceConfigUi()->show(); });
		menu->addSeparator();
		menu->addAction(QIcon(":/SmartControl/Resource/toolbar/delete.png"), A2T("删除(D)"), this, [=]() 
			{
				QMetaObject::invokeMethod(this, [=]()
					{
						emit sign_Delete(m_pNode);
					}, Qt::QueuedConnection);  // 关键：排队触发
			});
		menu->addSeparator();
		menu->addAction(A2T("属性"), this, [=]()
			{
				UPropertyNode dlg(m_pNode, this);  // this 是父窗口
				dlg.exec();
			});
		menu->exec(mapToGlobal(pos));
	}
}
